package com.yoomiito.app.adapter.home.reommend;

import android.content.Context;
import androidx.annotation.NonNull;

import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.yoomiito.app.R;
import com.yoomiito.app.model.bean.GoodsInfo;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.utils.Type;
import com.yoomiito.app.utils.UserInfoConfig;

import java.util.List;

/**
 * 首页---活动
 */
public class HomeRecommendAdapter extends DelegateAdapter.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private List<GoodsInfo> datas;
    private OnItemClickLisenter mOnItemClickLisenter;
    public HomeRecommendAdapter(Context context, LayoutHelper layoutHelper, List<GoodsInfo> datas) {
        mContext = context;
        mLayoutHelper = layoutHelper;
        this.datas = datas;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_goods_1, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        GoodsInfo info = datas.get(position);
        //设置title
        int resId = 0;
        //注释原因：1、sort字段类型出现异常 原为int 然后修改为long 在switch中不能使用
        //         2、这个类不再使用 没有删除是因为其他类使用了这个类的一个接口
//        switch (info.getSort()) {
//            case 0:
//                resId = R.mipmap.tb;
//                break;
//            case 1:
//                resId = R.mipmap.tmall;
//                break;
//        }
        setTitle(itemViewHolder.titleTv,info.getTitle(),resId);
        //优惠券
        setGiftAndCoupon(itemViewHolder.iv_2, itemViewHolder.ll_2, itemViewHolder.voucherTv, info.getCouponPrice());
        //奖--佣金  会员以上显示  分享赚同奖
        if (UserInfoConfig.getRoleId() > Type.LevelType.VIP_0){
            setGiftAndCoupon(itemViewHolder.iv_1, itemViewHolder.ll_1, itemViewHolder.bonusTv, info.getBuyAwardPrice());
        }else {
            itemViewHolder.iv_1.setVisibility(View.GONE );
            itemViewHolder.ll_1.setVisibility(View.GONE);
        }

        itemViewHolder.rearTv.setText(info.getDiscountPrice());
        if (UserInfoConfig.getRoleId()>= Type.LevelType.VIP_4){
            itemViewHolder.mShareLl.setVisibility(View.VISIBLE);
            itemViewHolder.shareTv.setText("分享赚¥" + info.getBuyAwardPrice());
        }else {
            itemViewHolder.mShareLl.setVisibility(View.GONE);
        }

        ImageLoader.getInstance().into(mContext, info.getImageUrl(), 4,itemViewHolder.goodsIv, R.drawable.goods_default);
        itemViewHolder.mView.setOnClickListener(v -> {
            if (mOnItemClickLisenter != null) {
                mOnItemClickLisenter.onClick(position,false);
            }
        });
        itemViewHolder.mShareLl.setOnClickListener(v -> {
            if (mOnItemClickLisenter != null) {
                mOnItemClickLisenter.onClick(position,true);
            }
        });
    }

    private void setTitle(TextView tv,String str,int ivResId) {
        tv.setText("");
//        SpannableString spanText = new SpannableString(""+str);
//        if (ivResId!=0){
//            Drawable d = mContext.getDrawable(ivResId);
//            d.setBounds(0, 0, DensityUtil.dp2px(22), DensityUtil.dp2px(14));
//            spanText.setSpan(new ImageSpan(d), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//        }
        tv.append(SpannableUtils.getIconAndText(str, ResUtil.getDrawable(ivResId)));
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public int getItemViewType(int position) {
        return 5;
    }

    private void setGiftAndCoupon(ImageView iv, View view, TextView tv, String value) {
//        boolean isCoupon = TextUtils.isEmpty(value)||"0".equals(value)||"0.0".equals(value)||"0.00".equals(value);
        boolean isCoupon = SpannableUtils.isEmptyOr0(value);
        iv.setVisibility(isCoupon ? View.GONE : View.VISIBLE);
        view.setVisibility(isCoupon ? View.GONE : View.VISIBLE);
        if (!isCoupon) tv.setText("¥" + value);
    }

    private class ItemViewHolder extends RecyclerView.ViewHolder {

        private TextView shareTv;
        private TextView rearTv;//券后价
        private TextView voucherTv;//券
        private TextView bonusTv;//奖
        private TextView titleTv;//商品名称
        private ImageView goodsIv;//商品图
        private View mView;
        private ImageView iv_1;
        private ImageView iv_2;
        private LinearLayout ll_1;
        private LinearLayout ll_2;
        private LinearLayout mShareLl;
        public ItemViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
            goodsIv = itemView.findViewById(R.id.iv_list);
            titleTv = itemView.findViewById(R.id.tv_list_title);
            bonusTv = itemView.findViewById(R.id.tv_bonus);
            voucherTv = itemView.findViewById(R.id.tv_voucher);
            rearTv = itemView.findViewById(R.id.tv_voucher_Rear);
            shareTv = itemView.findViewById(R.id.tv_share_bonus);
            iv_1 = itemView.findViewById(R.id.item_gift_iv_1);
            ll_1 = itemView.findViewById(R.id.item_gift_ll_1);
            iv_2 = itemView.findViewById(R.id.item_coupon_iv_2);
            ll_2 = itemView.findViewById(R.id.item_coupon_ll_2);
            mShareLl = itemView.findViewById(R.id.tv_share_ll);
        }
    }

    public void setOnItemClickLisenter(OnItemClickLisenter onItemClickLisenter) {
        mOnItemClickLisenter = onItemClickLisenter;
    }

    public interface OnItemClickLisenter {
        void onClick(int position,boolean isShare);
    }
}
